<template>
  <div class="user-detail">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>用户详情</span>
          <el-button @click="goBack">返回</el-button>
        </div>
      </template>
      
      <el-descriptions :column="1" border>
        <el-descriptions-item label="用户ID">{{ user.id }}</el-descriptions-item>
        <!-- <el-descriptions-item label="用户名">{{ user.username }}</el-descriptions-item> -->
        <el-descriptions-item label="昵称">{{ user.nickname }}</el-descriptions-item>
        <!-- <el-descriptions-item label="邮箱">{{ user.email }}</el-descriptions-item> -->
        <el-descriptions-item label="角色">
          <el-tag v-for="role in user.roles" :key="role" style="margin-right: 5px;">
            {{ role }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="权限">
          <el-tag v-for="permission in user.permissions" :key="permission" style="margin-right: 5px;">
            {{ permission }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ formatDate(user.created_at) }}</el-descriptions-item>
        <!-- <el-descriptions-item label="最后登录">{{ formatDate(user.last_login) }}</el-descriptions-item> -->
      </el-descriptions>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { getUserDetail } from '@/api/user'
import { formatDate } from '@/utils/format'

interface User {
  id: number
  username: string
  nickname: string
  email: string
  roles: string[]
  permissions: string[]
  created_at: string
  last_login: string
}

const route = useRoute()
const router = useRouter()
const user = ref<User>({
  id: 0,
  username: '',
  nickname: '',
  email: '',
  roles: [],
  permissions: [],
  created_at: '',
  last_login: ''
})

const goBack = () => {
  router.go(-1)
}

const loadUserDetail = async () => {
  try {
    const userId = route.params.id as string
    const response = await getUserDetail(userId)
    user.value = response.data
  } catch (error) {
    ElMessage.error('获取用户详情失败')
    console.error(error)
  }
}

onMounted(() => {
  loadUserDetail()
})
</script>

<style scoped lang="scss">
.user-detail {
  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
}
</style>